#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QSystemTrayIcon>  // 添加系统托盘头文件
#include <QCloseEvent>      // 添加关闭事件头文件
#include <CameraManager/cameraservice.h>
#include <AlgManager/algorithmservice.h>
#include <DeviceManager/devicecommservice.h>
#include <DatabaseManager/databaseservice.h>
#include <Common/syslogmanager.h>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void closeEvent(QCloseEvent *event) override;  // 重写关闭事件

private slots:
    // 按钮点击事件
    void on_btnLoadImage_clicked();
    void on_btnPreProcess_clicked();
    void on_btnDefectDetection_clicked();
    void on_btnPathPlanning_clicked();
    void on_btnExecutePath_clicked();
    void on_btnSingleShot_clicked();

    void on_btnRegression_clicked();
    void on_btnAlgRunAll_clicked();
    void on_btnUploadCommand_clicked();
    void on_btnRunCommand_clicked();
    void on_btnClearCommand_clicked();
    void on_btnCommandRunAll_clicked();

    // 菜单事件
    void on_actionShowControl_triggered();
    void on_actionShowResults_triggered();
    void on_actionAbout_triggered();
    void on_actionExit_triggered();

    // 服务日志处理
    void onLogReceived(const QDateTime& time, int logType, int infoType, const QString& content);
    void onLogMessageReceived(const QString& message);
    void on_btnClearLog_clicked();
    void on_Bt_save_clicked();

    // 新增任务处理槽函数
    void onTaskReceived(const HardwareTaskQueue& task);
    void onTaskProcessed(const TaskExecutionResult& result);

    // 系统托盘槽函数
    void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void onShowMainWindow();
    void onQuitApplication();

private:
    // 任务处理函数
    void processTask(const HardwareTaskQueue& task);
    void executeTask(const HardwareTaskQueue& task);

    // 工具函数
    QVariantMap parseTaskParams(const QJsonObject& taskParams);
    QString getFixtureStatusText(const QString& fixtureStatus);

    // 系统托盘相关函数
    void createTrayIcon();
    void createTrayMenu();

private:
    void setupUI();
    void setupServices();
    void updateParameterDisplay();
    void updateStatusIndicators();
    void updateIndicatorStyle(QLabel *label, bool status);
    void loadDefaultParameters();
    void saveParameters();
    void logMessage(const QString& message);

private:
    void setupFixtureComboBox();
    void updateFixtureControlState();
    // 设备通信相关
    void setupDeviceConnections();

    // 定时查询相关
    void setupQueryTimer();
    void onQueryTimeout();
    void startPeriodicQuery();
    void stopPeriodicQuery();
    void executeSingleQuery();

    // 设备通信状态
    bool m_connected = false;
    QVector<QString> m_commands;

    // 定时器查询
    QTimer *m_queryTimer;
    bool m_queryEnabled = false;
    int m_queryInterval = 1000;

    // 系统托盘相关
    QSystemTrayIcon *m_trayIcon;
    QMenu *m_trayMenu;
    bool m_isQuitting = false;  // 标记是否真正退出

private:
    Ui::MainWindow *ui;

    // 业务服务
    CameraService* m_cameraService;
    AlgorithmService* m_algorithmService;
    DatabaseService* m_databaseService;
    DeviceCommService* m_deviceCommService;

    // 图像显示
    ImageWindow *m_imageWindow;

private slots:
    void onAutoExecutionStarted();
    void onAutoExecutionProgress(int currentGroup, int totalGroups, const QString& status);
    void onAutoExecutionFinished(bool success, const QString& message);
    void onAutoExecutionStopped();

    void on_btnAutoExecuteAll_clicked();
    void on_btnStopAutoExecution_clicked();
    void on_btStartQuery_clicked();

    void on_btnSaveRate_clicked();


    void on_btnStartSpindle_clicked();

    void on_btnStopSpindle_clicked();

    void on_btnSetDO_clicked();

    void on_btnResetDO_clicked();

    void on_btnReleaseAllFixture_clicked();

    void on_btnClampAllFixture_clicked();


    void on_btnLoadMaterial_clicked();

    void on_btnUnloadMaterial_clicked();

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_btnUpdateParameters_clicked();


private:
    void setupAutoExecutionConnections();
    void setAutoExecutionUIState(bool executing);
private:
    // 夹具状态检查辅助方法
    bool isAllFixturesClamped(const QString& fixtureStatus);
    bool isAllFixturesReleased(const QString& fixtureStatus);
private:
    QTimer* m_loadMaterialTimer;
    int m_loadMaterialCheckCount;

    void startLoadMaterialMonitoring();

};

#endif // MAINWINDOW_H
